/**
 * 
 */
package com.dsteiner.xelavos.client;


import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.PopupOverlayWidget;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.ImageExt;
import com.dsteiner.widgets.client.Widgets.InlineHTMLExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.SimplePanelExt;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.ChangeImageWidget.ChangeImageModel;
import com.dsteiner.xelavos.client.ChangeableImage.ChangeableImageModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.controller.CreateObjectController;
import com.dsteiner.xelavos.client.controller.ImageSearchImpl;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.model.CreateObjectModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

public class CreateObjectWidget extends LazyWidget {

	private CreateObjectController createObjectController = new CreateObjectController();
	private final CreateObjectModel createObjectModel;
	private final AuthenticationModel authenticationModel;

	public CreateObjectWidget(CreateObjectModel createObjectModel, 
			AuthenticationModel authenticationModel) {
		this.createObjectModel = createObjectModel;
		this.authenticationModel = authenticationModel;
	}

	@Override
	public Widget createContentWidget() {
		ButtonExt createObjectButton = new ButtonExt("crear");
		FlowPanel mainPanel = new FlowPanel();
	
		ChangeImageModel changeImageModel = new ChangeImageModel();
		ChangeImageWidget changeFotoWidget = new ChangeImageWidget(changeImageModel);
		changeImageModel.setMaxFileSize(Constants.MAX_FILE_SIZE);
		changeImageModel.setTitle("agregar foto (archivo con formato jpg o png)");
		PopupOverlayModel fileUploadPopupOverlayModel = new PopupOverlayModel();
		PopupOverlayWidget popupOverlayWidget = new PopupOverlayWidget(fileUploadPopupOverlayModel);
		fileUploadPopupOverlayModel.setContentWidget(changeFotoWidget);
		HTMLExt changeImageLink = new HTMLExt("<a href=\"javascript:void(0);\">cambia foto</a>");
		HTMLExt requiredFieldsHtml = new HTMLExt("* = campos obligatorios");
		SimplePanelExt contentPanel = new SimplePanelExt();
		ChangeableImageModel changeableImageModel = new ChangeableImageModel();
		ChangeableImage image = new ChangeableImage(changeableImageModel);
		InlineHTMLExt titleHtml = new InlineHTMLExt();
		ImageExt icon = new ImageExt();
		
		mainPanel.add(icon);
		mainPanel.add(titleHtml);
		mainPanel.add(contentPanel);
		mainPanel.add(image);
		mainPanel.add(changeImageLink);
		mainPanel.add(createObjectButton);
		mainPanel.add(requiredFieldsHtml);
		
		createObjectButton.setTabIndex(99);
		image.addStyleName(XelavosResources.INSTANCE.css().mainImg());
		setStyleName(XelavosResources.INSTANCE.css().createObjectWidget());
		titleHtml.addStyleName(XelavosResources.INSTANCE.css().titleHtml());
		requiredFieldsHtml.addStyleName(XelavosResources.INSTANCE.css().requiredFieldsHtml());

		createObjectController.registerImageSearch(new ImageSearchImpl());
		createObjectController.registerJsUtil(JsUtil.getInstance());
		createObjectController.registerIcon(icon);
		createObjectController.registerCreateObjectWidget(this);
		createObjectController.registerCreateObjectModel(createObjectModel);
		createObjectController.registerCreateObjectButton(createObjectButton);
		createObjectController.registerTitleHtml(titleHtml);
		createObjectController.registerAuthenticationModel(authenticationModel);
		createObjectController.registerChangeImageModel(changeImageModel);
		createObjectController.registerFileUploadPopupOverlayModel(fileUploadPopupOverlayModel);
		createObjectController.registerChangeImageLink(changeImageLink);
		createObjectController.registerContentPanel(contentPanel);
		createObjectController.registerChangeableImageModel(changeableImageModel);
		
		return mainPanel;
	}
}